package com.dong.coordinator

import android.os.Bundle
import android.view.View
import android.view.animation.*
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.appbar.AppBarLayout.OnOffsetChangedListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_layout.*
import kotlinx.android.synthetic.main.title_layout.*
import kotlin.math.abs

class MainActivity : AppCompatActivity() {
    private val titles = arrayOf("推荐", "房源", "体验", "攻略")
    var mLayoutState = CollapsingToolbarLayoutState.EXPANDED
    private var mHideSet: AnimationSet? = null
    private var mShowSet: AnimationSet? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initTabLayout()
        initAnimationParam()
        initAppBarLayout()
    }

    private fun initAnimationParam() {
        showAnim()
        hideAnim()
    }

    private fun initTabLayout() {
        for (title in titles) {
            tablayout.addTab(tablayout.newTab().setText(title))
        }
    }

    private fun initAppBarLayout() {
        appBarLayout.addOnOffsetChangedListener(OnOffsetChangedListener { appBarLayout, verticalOffset ->
            if (verticalOffset == 0) {
                if (mLayoutState != CollapsingToolbarLayoutState.EXPANDED) {
                    mLayoutState = CollapsingToolbarLayoutState.EXPANDED //修改状态标记为展开
                    titleLL.visibility = View.GONE
                }
            } else if (abs(verticalOffset) >= appBarLayout.totalScrollRange) {
                if (mLayoutState != CollapsingToolbarLayoutState.COLLAPSED) {
                    contentLL.startAnimation(mHideSet)
                    contentLL.visibility = View.GONE //隐藏
                    titleLL.startAnimation(mShowSet)
                    titleLL.visibility = View.VISIBLE
                    mLayoutState = CollapsingToolbarLayoutState.COLLAPSED //修改状态标记为折叠
                }
            } else {
                if (mLayoutState != CollapsingToolbarLayoutState.MIDDLE) {
                    if (mLayoutState == CollapsingToolbarLayoutState.COLLAPSED) {
                        contentLL.startAnimation(mShowSet)
                        contentLL.visibility = View.VISIBLE //由折叠变为中间状态时隐藏
                        titleLL.startAnimation(mHideSet)
                        titleLL.visibility = View.GONE
                    }
                    mLayoutState = CollapsingToolbarLayoutState.MIDDLE //修改状态标记为中间
                }
            }
        })
    }

    /**
     * 定义出CollapsingToolbarLayout展开、折叠、中间三种状态
     */
    enum class CollapsingToolbarLayoutState {
        EXPANDED, COLLAPSED, MIDDLE
    }

    private fun showAnim() {
        mShowSet = AnimationSet(true)
        val scaleAnimation = ScaleAnimation(0.0f, 1.0f, 1.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        val alphaAnimation = AlphaAnimation(0.0f, 1.0f)
        val showAction = TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f)
        mShowSet!!.addAnimation(alphaAnimation)
        mShowSet!!.addAnimation(scaleAnimation)
        mShowSet!!.duration = 500
    }

    private fun hideAnim() {
        mHideSet = AnimationSet(true)
        mHideSet!!.interpolator = LinearInterpolator()
        val alphaAnimation = AlphaAnimation(1.0f, 0.0f)
        val hiddenAction = TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f)
        mHideSet!!.addAnimation(alphaAnimation)
        mHideSet!!.addAnimation(hiddenAction)
        mHideSet!!.duration = 300
    }
}